package com.devil.traffic.tools;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.devil.traffic.activity.R;
import com.devil.traffic.service.TrafficService;

public class Tool {

    /*******************
     * 网络连接出错，提示对话框
     * 
     * @param context
     */
    public static void alerNetErr(final Context context) {

        // 对话框
        AlertDialog.Builder ab = new AlertDialog.Builder(context);
        ab.setTitle(R.string.NoRouteToHostException);
        ab.setMessage(R.string.NoSignalException);
        // 设置操作对象
        ab.setPositiveButton("设置网络",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 取消对话框
                        dialog.cancel();
                        // 打开网络设置Activity
                        Intent it = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
                        context.startActivity(it);
                    }
                });
        ab.setNegativeButton("取消",
                new OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 取消对话框
                        dialog.cancel();
                        // 退出程序
                        TrafficService.exitApp(context);
                    }
                });
        // 显示
        ab.create().show();
    }

    /************************
     * 提示对话框，APP退出确认对话框
     * 
     * @param context
     */
    public static void promptExit(final Context context) {

    	System.out.println("退出退出退出");
//        // 通过Inflater对象把布局文件压缩为视图
//        LayoutInflater flater = LayoutInflater.from(context);
//        View exitView = flater.inflate(R.layout.exitdialog, null);
//
//        AlertDialog.Builder builder = new AlertDialog.Builder(context);
//        // 改变对话框的默认布局，用已有视图来覆盖
//        builder.setView(exitView);
//
//        builder.setPositiveButton(R.string.confirm_exit,
//                new DialogInterface.OnClickListener() {
//
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        exitApp(context);
//                    }
//                });
//
//        builder.setNegativeButton(R.string.apn_is_wrong1_exit, null);
//
//        // 显示对话框
//        builder.show();
	    	new AlertDialog.Builder(context)
			.setIcon(R.drawable.bluedot)
			.setTitle("信息确认")
			.setMessage("确定要退出吗？")
			.setNegativeButton("取消", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
				}
			})
			.setPositiveButton("确定", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					TrafficService.exitApp(context);
				}
			}).show();
    }

	public static ProgressDialog getProgressDialog(Context context,String title,String message){
		ProgressDialog progressDialog = new ProgressDialog(context);
		progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progressDialog.setTitle(title);
		progressDialog.setMessage(message);
		progressDialog.setIndeterminate(false);
		progressDialog.setCancelable(true);
		return progressDialog;
	}
	public static void saveDate(String key,String value,Context context){
		System.out.println("pppppppppp"+key+"@"+value);
		SharedPreferences sharedPreferences = context.getSharedPreferences("HouseClub", Context.MODE_WORLD_WRITEABLE);
		Editor editor = sharedPreferences.edit();//获取编辑器
		editor.putString(key,value);
		editor.commit();//提交修改
	}
	
	public static boolean checkNetWorkStatus(Context context){
		  boolean result;
		  ConnectivityManager cm=(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		  NetworkInfo netinfo = cm.getActiveNetworkInfo();
		  if ( netinfo !=null && netinfo.isConnected() ) {
		   result=true;
		  }else{
		   result=false;
		  }
		  return result;
		  }

	public static boolean goodNet(Context context) {
		ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkinfo = manager.getActiveNetworkInfo();
		if (networkinfo == null || !networkinfo.isAvailable()) {
			return false;
		}
		return true;
	}
}
